﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using GameServer.Managers;
using GameServer.Networking.Packets;

namespace GameServer.Networking.Handlers
{
    class HANDLE_COSTUME_BUY : PacketHandler
    {
        public override void Handle(Virtual_Objects.User.virtualUser User)
        {
            try
            {
                int OPCode = int.Parse(getNextBlock());
                int Period = int.Parse(getBlock(2));
                string Code = getBlock(4);
                int[] convertDays = new int[4] { 3, 7, 15, 30 };
                virtualItem Item = ItemManager.getItem(Code);
                if (Item != null)
                {
                    if (User.Cash < Item.Cash)
                    {
                        User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.NotEnoughDinar, "NULL"));
                    }
                    /*else if (LevelCalculator.getLevelforExp(User.Exp) < Item.Level && User.Rank < 3)
                    {
                        User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.LevelLow, "NULL"));
                    }*/
                    else
                    {
                        User.Cash -= Item.Cash;
                        DB.runQuery("UPDATE users SET cash='" + User.Cash + "' WHERE id='" + User.UserID + "'");
                        int InventorySlot = -1;
                        for (int I = 0; I < 30; I++)
                        {
                            if (User.Costume[I] == null) { InventorySlot = I; break; }
                        }
                        if (InventorySlot >= 0)
                        {
                            User.AddCostume(Code, convertDays[Period]);
                            User.LoadItems();
                            // Costume Inventory //
                            StringBuilder Costumes = new StringBuilder(); int CostumeCount = 0;
                            for (int I = 0; I < 30; I++)
                            {
                                if (User.Costume[I] != null)
                                {
                                    CostumeItem _Inv = User.Costume[I];
                                    TimeSpan ExpireTime = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(_Inv._StartTime);

                                    Costumes.Append(_Inv._Code.ToUpper() + "-3-3-" + _Inv._StartTime + "-0-0-0-0-0-9999-9999,");/*+ ExpireTime.TotalSeconds +*/
                                    CostumeCount++;
                                }
                            }

                            for (int I = 0; I < (60 - CostumeCount); I++)
                            {
                                Costumes.Append("^,");
                            }

                            User.send(new PACKET_COSTUME_BUY(User, Costumes.ToString().Remove(Costumes.ToString().Length - 1)));
                            User.send(new Packets.PACKET_CREDITS(User));
                            Log.WriteLine(User.Nickname + " has bought [" + Item.WeaponCode.ToUpper() + "-" + Item.Name + "] for " + convertDays[Period] + "days.");
                        }
                        else
                        {
                            User.send(new PACKET_ITEMSHOP(PACKET_ITEMSHOP.ErrorCodes.InventoryFull, "NULL"));
                        }
                    }
                }
                else
                {
                    User.send(new Packets.PACKET_ITEMSHOP(Packets.PACKET_ITEMSHOP.ErrorCodes.CannotBeBougth, "NULL"));
                }
            }
            catch (Exception ex)
            {
                Log.WriteError(ex.Message);
            }
        }
    }
}
